﻿namespace nCodeGeneratingTool
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    public class CodegGenHelper
    {
        #region Methods

        public List<CommandData> GenerateCommands(string XsltFolder)
        {
            List<string> commands = File.ReadAllLines(XsltFolder + "\\nCodeGeneratorFiles\\Operations.txt").ToList();
            List<CommandData> cdata = new List<CommandData>();
            int i = 0;

            foreach (string com in commands)
            {
                string[] data = com.Split('|');
                CommandData cd = new CommandData(Convert.ToInt32(data[0]), data[1], data[2], data[3]);
                cdata.Add(cd);
                //Console.WriteLine((i + 1).ToString("00000") + " - " + cd.DisplayName);
                //i = i + 1;
            }
            //Console.WriteLine("-1 to exit");
            return cdata;
        }

        public string GenerateOutputfile(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, string startComment, string endComment, XMLProperties propCollection)
        {
            try
            {
                string[] Lines = bstrInputFileContents.Split('\n');
                List<CommandData> commands = GenerateCommands(propCollection.Properties["XSLTFileCollectionPath"].ToString());
                string serverDetails = propCollection.Properties["NCGDBConnectionString"]; //Lines.Where(t => t.Trim().StartsWith("..DB")).First().Replace("..DB:", "");

                for (int i = 0; i < Lines.Length; i++)
                {
                    if (Lines[i].Trim().StartsWith("."))
                    {
                        try
                        {
                            string operationId = Lines[i].Substring(0, Lines[i].IndexOf(':')).Replace(".", "");
                            int id = Convert.ToInt32(operationId.Trim());
                            CommandData ec = commands.Where(t => t.Id == id).First();
                            if (id > 0)
                            {
                                try
                                {
                                    string operationCommand = Lines[i].Substring(Lines[i].IndexOf(':') + 1).Replace(".", "");
                                    Lines[i] = startComment + "Auto Generated by Nipesh Shah's Auto Generating Free Tool" + endComment + "\n\n" + startComment + ec.DisplayName + endComment + "\n" + CodeGen.કોડબનાવો(ec.CommandName, ec.XsltFile, operationCommand.Trim(), serverDetails.Trim(), propCollection.Properties["XSLTFileCollectionPath"]);
                                }
                                catch (Exception ex)
                                {
                                    Lines[i] = "LOG 1" + Lines[i] + " : " + ex.Message + " : " + ex.StackTrace + " : " + ex.InnerException + " : " + ex.Source;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Lines[i] = "LOG 2" + Lines[i] + " : " + ex.Message + " : " + ex.StackTrace + " : " + ex.InnerException + " : " + ex.Source;
                        }
                    }
                }
                //List<string> cmd = new List<string>();
                //foreach (CommandData cd in commands)
                //{
                //    cmd.Add(cd.Id + " : " + cd.DisplayName + " : " + cd.CommandName + " : " + cd.XsltFile);
                //}
                return String.Join("\n", Lines);// +"\n\n" + String.Join("\n", cmd.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion Methods
    }
}